//
// Created by gummybear on 24-7-1.
//

#include "../include/question.h"

template <typename T>
void printVector(const std::vector<T> &data) {
    std::cout << "{";
    for (int i = 0; i < data.size() - 1; i++) {
        std::cout << data[i] << ", ";
    }
    std::cout << data.back();
    std::cout << "}\n";
}

void printBinaryTree(const TreeNode* root) {
    std::queue<const TreeNode*> que;
    que.push(root);
    while (!que.empty()) {
        size_t que_size = que.size();
        for (int i = 0; i < que_size; i++) {
            const TreeNode* cur = que.front();
            que.pop();
            if (cur == nullptr) {
                std::cout << "&, ";
            } else {
                std::cout << cur->val << ", ";
                que.push(cur->left);
                que.push(cur->right);
            }
        }
    }
}

ListNode* buildList(const std::vector<int> &data) {
    auto head = new ListNode{0};
    auto cur = head;
    for (const auto &val: data) {
        cur->next = new ListNode(val);
        cur = cur->next;
    }
    return head;
}

void test_No1() {
    std::cout << "\n-----------------No1-------------------------\n";

    std::vector<std::vector<int>> data = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
    int target_1 = 7;
    assert(Solution_No1::findInt(data, target_1));
    int target_2 = 3;
    assert(!Solution_No1::findInt(data, target_2));
}

void test_No2() {
    std::cout << "\n-----------------No2-------------------------\n";

    std::string origin_data{"We are happy"};
    assert((Solution_No2::replaceSpace(origin_data) == "We%20are%20happy"));
}

void test_No3() {
    std::cout << "\n-----------------No3-------------------------\n";

    std::vector<int> vec_data = {1, 2, 3, 4, 5};
    auto head = buildList(vec_data);

    printVector(Solution_No3::printListFromTailToHead(head->next));
}

void test_No4() {
    std::cout << "\n-----------------No4-------------------------\n";

    std::vector<int> preOrder{1, 2, 4, 7, 3, 5, 6, 8};
    std::vector<int> vinOrder{4, 7, 2, 1, 5, 3, 8, 6};
    const auto result = Solution_No4::reConstructBinaryTree(preOrder, vinOrder);
    printBinaryTree(result);
}

void test_No6() {
    std::cout << "\n-----------------No6-------------------------\n";

    std::vector<int> data{3, 4, 5, 1, 2};
    std::cout << Solution_No6::minNumberInRotateArray(data) << "\n";
}

void test_No12() {
    std::cout << "\n-----------------No12-------------------------\n";

    double data1{2.0000};
    int exp1{3};
    std::cout <<  Solution_No12::power(data1, exp1) << std::endl;

    double data2{2.10000};
    int exp2{3};
    std::cout << Solution_No12::power(data2, exp2) << std::endl;

    double data3{2.00000};
    int exp3{-2};
    std::cout << Solution_No12::power(data3, exp3) << std::endl;
}

void test_No13() {
    std::cout << "\n-----------------No13-------------------------\n";

    std::vector<int> data{1, 2, 3, 4, 5, 6};
    printVector(Solution_No13::reOrderArray(data));
}

void test_No14() {
    std::cout << "\n-----------------No14-------------------------\n";

    ListNode* data_head = buildList(std::vector<int>{1, 2, 3, 4, 5});
    std::cout << Solution_No14::findKthToTail(data_head, 1);
    std::cout << Solution_No14::findKthToTail(data_head, 2);
}

int main() {
    std::cout << "Hello sword!" << std::endl;
    test_No1();
    test_No2();
    test_No3();
    test_No4();
    test_No6();
    test_No12();
    test_No13();
    test_No14();

    return 0;
}

